//package com.pengzy.core.gateway_security;
//
//import com.pengzy.comment.web.result.JsonResult;
//import com.pengzy.comment.web.webFluxUtils.ResponseResult;
//import org.springframework.http.HttpStatus;
//import org.springframework.security.authentication.*;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.web.server.WebFilterExchange;
//import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
//import org.springframework.stereotype.Component;
//import org.springframework.util.MultiValueMap;
//import reactor.core.publisher.Mono;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author xiaozhi 2023/7/18
// */
//@Component
//public class AuthenticationFailureConfig  implements ServerAuthenticationFailureHandler {
//
//
//
//
//    @Override
//    public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
//        webFilterExchange.getExchange().getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//        Map<String, String> responseMap = new HashMap<>();
//        responseMap.put("code", "failure");
//        if (exception instanceof UsernameNotFoundException) {
//            webFilterExchange.getExchange().getResponse().setStatusCode(HttpStatus.OK); //登录页面返回200，正常报错提示
//            responseMap.put("msg", "用户不存在 " + exception.getMessage());
//        } else if (exception instanceof BadCredentialsException) {
//            webFilterExchange.getExchange().getResponse().setStatusCode(HttpStatus.OK);//登录页面返回200，正常报错提示
//            Mono<MultiValueMap<String, String>> formData = webFilterExchange.getExchange().getFormData();
////                        new Thread(()->{
////                            String username = Objects.requireNonNull(formData.block()).get("username").get(0).toString();
////                            userService.lockUserByErrorPs(username); //密码输错次数+1
////                        }).start();
//            responseMap.put("msg", "密码错误 " + exception.getMessage());
//        } else if (exception instanceof LockedException) {
//            responseMap.put("msg", "用户锁定 " + exception.getMessage());
//        } else if (exception instanceof AccountExpiredException) {
//            responseMap.put("msg", "账户过期 " + exception.getMessage());
//        } else if (exception instanceof DisabledException) {
//            responseMap.put("msg", "账户不可用 " + exception.getMessage());
//        } else {
//            responseMap.put("msg", "系统错误 " + exception.getMessage());
//        }
//        //responseMap.put("msg",responseMap.get("msg") + "，请重新登录");
//        //responseMap.put("msg", exception.getMessage());
//        return ResponseResult.writeWith(webFilterExchange.getExchange(), JsonResult.error(responseMap.get("msg") + "，请重新登录"));
//    }
//}
